﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Kyozy.Wke;

namespace ICSharpCode.WinFormsUI.Controls
{     
    public partial class DocoFormatControl : TextBoxBase
    {
        private IntPtr ControlHandle = IntPtr.Zero;

        private Color borderColor = SystemColors.ControlDark;      

        Kyozy.Wke.WebView webView = new Kyozy.Wke.WebView();

        public DocoFormatControl()
        {
            InitializeComponent();
            InitializeControl();
        }

        public override string Text
        {
            get
            {
                GetContent();
                return text;
            }
            set
            {
                text = value;
            }
        }        
             
        public void InitializeControl()
        {
            Kyozy.Wke.WebView.Init();  
         
            //this.webBrowser1.Url = new System.Uri(Application.StartupPath + "\\kindeditor\\index.html", System.UriKind.Absolute);
            //this.webBrowser1.ObjectForScripting = this;  

            webView.Bind(this.WebBrowserView, false);

            webView.OnDocumentReady += new EventHandler<DocumentReadyEventArgs>(OnDocumentReady);
            webView.OnLoadingFinish += new EventHandler<LoadingFinishEventArgs>(OnLoadingFinish);

            WebViewLoadFile("\\kindeditor\\index.html");

            //JS.BindFunction("GetContent", new wkeJSNativeFunction(GetContent), 0);
        }

        public void GetContent()
        {
            var intptr = webView.WebViewHandle;
            if (intptr == IntPtr.Zero)
                return;
            this.text = JS.ToStr(IntPtr.Zero, webView.RunJS("getContent();"));                
        }

        public void OnDocumentReady(object sender, DocumentReadyEventArgs args)
        {
            //
        }

        public void OnLoadingFinish(object sender, LoadingFinishEventArgs args)
        {
            if (!string.IsNullOrEmpty(text))
            {
                var value = text.Replace('\n', ' ');
                webView.RunJS("setContent('" + value + "');");
            }           
        }

        public void WebViewLoadFile(string Path)
        {
            string address = Application.StartupPath + Path;            
            webView.LoadFile(address);
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (webView.WebViewHandle == IntPtr.Zero)
            {
                webView.Bind(this.WebBrowserView, false);
                WebViewLoadFile("\\kindeditor\\index.html");
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
              borderColor, 1, ButtonBorderStyle.Solid, //左边
              borderColor, 1, ButtonBorderStyle.Solid, //上边
              borderColor, 1, ButtonBorderStyle.Solid, //右边
              borderColor, 1, ButtonBorderStyle.Solid);//底边
        }

        public override void ReleaseDispose()
        {
            webView.UnBind();
            base.ReleaseDispose();           
        }

    }
}
